package com.qububu.emporium.shoppingCart.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.qububu.db.dao.impl.ShoppingCartDaoImpl;
import com.qububu.emporium.shoppingCart.dto.CartSaveGoodsMessageDto;
import com.qububu.emporium.shoppingCart.service.ShoppingCartService;

@Service
public class ShoppingCartServiceImpl implements ShoppingCartService {

	@Resource
	private ShoppingCartDaoImpl cartDao;

	public CartSaveGoodsMessageDto getCartSaveGoodsMessageByGoodsId(
			String goodsId) {
		int defaultFirstSaveGoodsQuantity = 1;
		CartSaveGoodsMessageDto goods = cartDao.getCartSaveGoodsMessageByGoodsId(goodsId);
		goods.setGoodsQuantity(defaultFirstSaveGoodsQuantity);
		return goods;
	}

	public List<CartSaveGoodsMessageDto> getCartGoodsList(
			Map<String, CartSaveGoodsMessageDto> map) {
		List<CartSaveGoodsMessageDto> goodsList = new ArrayList<CartSaveGoodsMessageDto>();
		for (Entry<String, CartSaveGoodsMessageDto> entry : map.entrySet()) {
			goodsList.add(entry.getValue());
		}
		
		return goodsList;
	}

	/**
	 * 将数据转换成goodsList和商品总价
	 * */
	/*public Map<String, Object> getCartResultMap(
			Map<String, CartSaveGoodsMessageDto> map) {
		
		Map<String, Object> returnMap = new HashMap<String, Object>();
		List<CartSaveGoodsMessageDto> goodsList = new ArrayList<CartSaveGoodsMessageDto>();
		double quantity = 0;
		for (Entry<String, CartSaveGoodsMessageDto> entry : map.entrySet()) {
			CartSaveGoodsMessageDto goods = entry.getValue();
			quantity += (goods.getGoodsQuantity() * Double.parseDouble(goods.getGoodsPrice())); 
			goodsList.add(goods);
		}
		returnMap.put("goodsList", goodsList);
		returnMap.put(SystemConstants.CART_TOTAL_PRICE_KEY, quantity);
		
		return returnMap;
	}*/
	
}
